package cn.otrue.patienthealthmanager.adapter;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.ChatModle;
import cn.otrue.patienthealthmanager.utils.CommonUtil;
import cn.otrue.patienthealthmanager.utils.DensityUtil;
import cn.otrue.patienthealthmanager.utils.MyConst;

import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class DoctorCharAdpater extends BaseAdapter {

	private List<ChatModle> data;
	private LayoutInflater inflater;
	private Context context;
	private static boolean playState = false; // 播放状态
	private MediaPlayer media;
	private String in_head_image;
	private String out_head_image;
	private AnimationDrawable animationDrawable;

	public void ImageSet(String imgurl, ImageView img_headPhoto) {

		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_user_photo)
				.showImageOnFail(R.drawable.iv_user_photo)
				.showImageForEmptyUri(R.drawable.iv_user_photo)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565)
				.displayer(
						new RoundedBitmapDisplayer(DensityUtil.dip2px(context,
								40))).build();
		ImageLoader.getInstance().displayImage(imgurl, img_headPhoto, options);
	}

	public DoctorCharAdpater(List<ChatModle> data, Context context,
			String in_head_image, String out_head_image) {
		super();
		this.data = data;
		this.context = context;
		this.in_head_image = in_head_image;
		this.out_head_image = out_head_image;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final ChatModle eachChat = data.get(arg0);

		if (eachChat.getChatType() == MyConst.CHAT_IN) {
			arg1 = this.inflater.inflate(R.layout.item_chat_in, null);

		} else {
			arg1 = this.inflater.inflate(R.layout.item_chat_out, null);

		}
		LinearLayout ll_chat = (LinearLayout) arg1.findViewById(R.id.ll_chat);
		TextView dateView = (TextView) arg1.findViewById(R.id.tv_chat_row_date);
		TextView msgView = (TextView) arg1.findViewById(R.id.tv_chat_row_msg);
		ImageView iv_chat_photo = (ImageView) arg1
				.findViewById(R.id.iv_chat_photo);
		ImageView iv_head_image = (ImageView) arg1
				.findViewById(R.id.iv_head_image);
		if (eachChat.getChatType() == MyConst.CHAT_IN) {
			ImageSet(in_head_image, iv_head_image);
		} else {
			ImageSet(out_head_image, iv_head_image);
		}

		final ImageView iv_chat_media = (ImageView) arg1
				.findViewById(R.id.iv_chat_media);

		if (!eachChat.getFilePath().equals("")) {
			final File file = new File(eachChat.getFilePath());

			if (file.getName().substring(file.getName().lastIndexOf(".") + 1)
					.equals("acc")) {
				iv_chat_media.setVisibility(View.VISIBLE);

				// String fileName = file.getName();
				// msgView.setText(fileName.split("-")[2]);
				msgView.setText(file.getName().split("-")[2] + "“");
				Log.e("TAG", msgView.getText().toString());

				ll_chat.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (animationDrawable != null
								&& animationDrawable.isRunning()) {
							animationDrawable.stop();
						}
						animationDrawable = (AnimationDrawable) iv_chat_media
								.getDrawable();

						animationDrawable.start();
						// 如果不是正在播放
						if (!playState) {
							media = new MediaPlayer();
							try {
								// 设置播放资源
								media.setDataSource(file.getAbsolutePath());
								// 准备播放
								media.prepare();
								// 开始播放
								media.start();

								animationDrawable.start();
								// timeText.setVisibility(View.GONE);
								// bar.setVisibility(View.VISIBLE);
								// bar.setMax(((int) media.getDuration()));
								// barUpdate();
								// 改变播放的标志位
								playState = true;

								// 设置播放结束时监听
								media.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

									@Override
									public void onCompletion(MediaPlayer mp) {
										if (playState) {
											playState = false;

										}
										// bar.setVisibility(View.GONE);
										animationDrawable.stop();

									}
								});
							} catch (IllegalArgumentException e) {
								e.printStackTrace();
							} catch (SecurityException e) {
								e.printStackTrace();
							} catch (IllegalStateException e) {
								e.printStackTrace();
							} catch (IOException e) {
								e.printStackTrace();
							}
						} else {
							// 如果正在播放
							if (media.isPlaying()) {
								media.stop();
								playState = false;
							} else {
								playState = false;
							}

						}

					}
				});

			} else {
				TextView iv_chat_photo_filter = (TextView) arg1
						.findViewById(R.id.iv_chat_photo_filter);
				iv_chat_media.setVisibility(View.GONE);
				msgView.setVisibility(View.GONE);
				iv_chat_photo.setVisibility(View.VISIBLE);

				int schedule = eachChat.getSchedule();

				if (eachChat.getChatType() == MyConst.CHAT_OUT) {
					if (schedule == 0) {
						iv_chat_photo_filter.setVisibility(View.VISIBLE);
						iv_chat_photo_filter.setText("0");
					} else if (schedule == 100) {
						iv_chat_photo_filter.setText("100");
						iv_chat_photo_filter.setVisibility(View.GONE);

					} else {
						iv_chat_photo_filter.setText(schedule + "");
					}
				} else {

				}

				// Bitmap bitmap1 = BitmapFactory.decodeFile(eachChat
				// .getFilePath());
				// bitmap1 = ThumbnailUtils.extractThumbnail(bitmap1, 100, 100,
				// ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

				// iv_chat_photo.setImageBitmap(bitmap1);
				BitmapUtils bu = new BitmapUtils(context);

				bu.display(iv_chat_photo,
						Uri.fromFile(new File(eachChat.getFilePath()))
								.toString());
				iv_chat_photo.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent();
						intent.setAction(android.content.Intent.ACTION_VIEW);
						intent.setDataAndType(
								Uri.fromFile(new File(eachChat.getFilePath())),
								"image/*");
						context.startActivity(intent);

					}
				});

			}
		} else {

			iv_chat_media.setVisibility(View.GONE);
			msgView.setVisibility(View.VISIBLE);
			msgView.setText(eachChat.getMessage());
		}

		dateView.setText(CommonUtil.getFormatDate("hh:mm",
				eachChat.getCreateTime()));

		return arg1;
	}
}
